var Global = {};
Global.TopWidgets = [];
Global.event  = { };

Global.event.Init = function(){
        Global.event.GlobalEvents = new Array();
        Global.event.GlobalTypes = { MouseDown:0,MouseUp:1}
        for(var e in Global.event.GlobalTypes){
           ei =  Global.event.GlobalTypes[e]
           Global.event.GlobalEvents[ei] = new Array()
        }
 }

Global.event.Init();

Global.event.GlobalEventHandler = function(type,p0,p1,p2,p3){
   EventArr = Global.event.GlobalEvents[type]       
   for (var i = 0; i < EventArr.length; i++) {
      EventArr[i](p0,p1,p2,p3)
   };
 }

Global.event.AddGlobalEvent =  function(EventType,CallBackFn){
     Global.event.GlobalEvents[EventType].push(CallBackFn)
}


function TopWidget(name,GoTw){
  this.Name = name
  this.GoTw = GoTw
  this.ChildWidget = new Array();
  
  this.AddChild = function(child){
     this.ChildWidget.push(child)
  }
  
  this.Find = function(path){
     arr = path.split("/")
     rt = this.GetByName(arr[0])
     for (var i = 1; i <arr.length;i++) {
      rt = rt.GetByName(arr[i])
     }
     return rt
  }

  this.GetByName = function(name){
      for (var i = 0; i < this.ChildWidget.length; i++) {
            if(this.ChildWidget[i].Name==name){
             return this.ChildWidget[i];
           }
      }
      return null
  }
  
  this.OnMouseDowns = new Array();
  this.MouseUpEvents = [];


  this.MouseDownClass = function  (nodeclass,fn) {
      NodeClass = this.GoTw.GetClass(nodeclass)
      ClassLen = NodeClass.Count()
      for (var i = 0; i < ClassLen; i++) {
          CurWidget = NodeClass.Get(i)
          this.OnMouseDowns[nodeclass] = fn
          AddEvent(CurWidget,"OnMouseDown",This,Global.event.GlobalTypes.MouseDown,nodeclass)
      }
  }

  this.MouseDown = function(Id,fn) {
     if(arguments.length==3)
     {
        node = arguments[0]
        this.OnMouseDowns[arguments[1]] = arguments[2]
        AddEvent(node,"OnMouseDown",This,Global.event.GlobalTypes.MouseDown,arguments[1])
     }
     else
     {
       node = this.GoTw.GetId(arguments[0])
       this.OnMouseDowns[arguments[0]] = fn;
       AddEvent(node,"OnMouseDown",This,Global.event.GlobalTypes.MouseDown,arguments[0])
    }
  }
  
  this.OnMouseDown = function(widget,Id){
     this.OnMouseDowns[Id](widget)
  }

  
  this.MouseUp = function(Id,fn){
    node = this.GoTw.GetId(Id)
    this.MouseUpEvents = new Array();
    this.MouseUpEvents[Id] = fn
    AddEvent(node,"OnMouseUp",This,Global.event.GlobalTypes.MouseUp,Id)
  }

  this.OnMouseUp = function(widget,Id){
      this.MouseUpEvents[Id](widget)
  }


}

